package com.onstar.cn.weight;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.Button;

/**
 * 
 * @author jun chen
 * @ClassName: CustomBtnDrawableLeftCenter 
 * @date 2015年12月3日 下午7:25:05 
 * @Description: 自定义的Button 使drawableLeft 居中显示
 */
public class CustomBtnDrawableLeftCenter extends Button{

	public CustomBtnDrawableLeftCenter(Context context) {
		super(context);
	}
	
	public CustomBtnDrawableLeftCenter(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public CustomBtnDrawableLeftCenter(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	
	@Override
	protected void onDraw(Canvas canvas) {
		
		Drawable[]  drawables = getCompoundDrawables();
		if(drawables != null)
		{
			Drawable drawableLeft = drawables[0];
			if(drawableLeft != null){
				  float textWidth = getPaint().measureText(getText().toString());
                int drawablePadding = getCompoundDrawablePadding();
                int drawableWidth = 0;
                drawableWidth = drawableLeft.getIntrinsicWidth();
                float bodyWidth = textWidth + drawableWidth + drawablePadding;
                canvas.translate((getWidth() - bodyWidth) / 2, 0);
			}
		}
		super.onDraw(canvas);
	}
}
